package com.graduation.smartparkingplatform.Controller;

import com.github.pagehelper.PageInfo;
import com.graduation.smartparkingplatform.Pojo.Opinion;
import com.graduation.smartparkingplatform.Response.Response;
import com.graduation.smartparkingplatform.Response.ResponseUtils;
import com.graduation.smartparkingplatform.Service.OpinionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/opinion")
public class OpinionController {
  @Autowired OpinionService opinionService;

  // 获取意见列表
  @GetMapping("/getOpinion")
  public Response getOpinion(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count) {
    try {
      PageInfo<Opinion> opinionList = opinionService.getOpinion(page, count);
      return ResponseUtils.buildSuccess(opinionList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 模糊搜索
  @GetMapping("/searchOpinion")
  public Response searchOpinion(@RequestParam("opinionId") Integer opinionId) {
    try {
      Opinion opinion = opinionService.searchOpinionById(opinionId);
      return ResponseUtils.buildSuccess(opinion);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 筛选处理/未处理意见
  @GetMapping("/OpinionStatus")
  public Response searchOpinionStatus(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count,
      @RequestParam("status") Boolean status) {
    try {
      PageInfo<Opinion> opinionList = opinionService.searchOpinionStatus(page, count, status);
      return ResponseUtils.buildSuccess(opinionList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 时间正序
  @GetMapping("/OpinionByAsc")
  public Response OpinionByAsc(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count) {
    try {
      PageInfo<Opinion> opinionList = opinionService.OpinionByAsc(page, count);
      return ResponseUtils.buildSuccess(opinionList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 时间倒序
  @GetMapping("/OpinionByDesc")
  public Response OpinionByDesc(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count) {
    try {
      PageInfo<Opinion> opinionList = opinionService.OpinionByDesc(page, count);
      return ResponseUtils.buildSuccess(opinionList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 改变意见状态
  @PostMapping("/changeOpinionStatus")
  public Response changeOpinionStatus(
      @RequestParam("opinionId") Integer opinionId, @RequestParam("status") Boolean status) {
    Boolean changeStatus = opinionService.changeOpinionStatus(opinionId, status);
    if (changeStatus) {
      return ResponseUtils.buildSuccess("修改处理状态成功");
    }
    return ResponseUtils.buildError("修改处理状态失败");
  }

  // 删除意见
  @PostMapping("/deleteOpinion")
  public Response deleteOpinion(@RequestParam("opinionId") Integer opinionId) {
    Boolean delete = opinionService.deleteOpinion(opinionId);
    if (delete) {
      return ResponseUtils.buildSuccess("删除意见成功");
    }
    return ResponseUtils.buildError("删除意见失败");
  }
}
